﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class FlyItemArtillery:FlyItem
{
    public enum HeadType
    {
        Collision,
        Explotion,
    }
    public float maxCharge{ get { return m_Volume; }}    //   最大装药量
    public float jetCharge { get { return m_JetCharge; } set { m_JetCharge = value; } } //   喷射装药量
    public float explotionCharge { get { return m_ExplotionCharge; } set { m_ExplotionCharge = value; } }//   爆弹装药量
    public int headMaterial { get { return m_HeadMaterial; } set { m_HeadMaterial = value; } }  //  弹头材料
    public int cartridgeHolder { get { return m_CartridgeHolder; } set { m_CartridgeHolder = value; } } // 弹夹量
    public HeadType headType = HeadType.Collision;//   弹头类型


    //public float m_MaxCharge = 0;
    public float m_JetCharge = 0;
    public float m_ExplotionCharge = 0;
    public int m_HeadMaterial = 0;
    public int m_CartridgeHolder = 1;
    public float m_Caliber = 1;


    public override JsonData Save()
    {
        JsonData root = base.Save();
        root["JetCharge"] = jetCharge;
        root["ExplotionCharge"] = explotionCharge;
        root["HeadMaterial"] = headMaterial;
        root["MaxCharge"] = maxCharge;
        return root;
    }
    public override bool Load(JsonData json)
    {
        base.Load(json);

        IDictionary<string, JsonData> jsond = json.ValueAsObject();
        //if (jsond.ContainsKey("MaxCharge"))
        //    maxCharge = jsond["MaxCharge"].ValueAsInt();
        if (jsond.ContainsKey("JetCharge"))
            jetCharge = (float)jsond["JetCharge"].ValueAsDouble();
        if (jsond.ContainsKey("ExplotionCharge"))
            explotionCharge = (float)jsond["ExplotionCharge"].ValueAsDouble();
        if (jsond.ContainsKey("HeadMaterial"))
            headMaterial = jsond["HeadMaterial"].ValueAsInt();
        CalcMass();
        return true;
    }
    public override float CalcMass()
    {
        float weight = base.CalcMass();
        weight += FlyItem.CalcChargeMass(jetCharge ); 
        if (headType == HeadType.Collision)
            weight += volume / 300f * headMaterial;
        else if (headType == HeadType.Explotion)
            weight += FlyItem.CalcChargeMass(explotionCharge);
        mass = weight;
        return mass;
    }
    public float CalcReloadFrequency()
    {
        float x = m_Mass ;
        return Mathf.Pow(x / m_CartridgeHolder, 2) + x + 0.2f;
    }
    public float CalcShotFrequency()
    {
        float x = m_Mass;
        return Mathf.Pow(x, 2) + x + 0.2f;
    }

    public float CalcEmitForce()
    {
        return jetCharge / m_Caliber / 5f;
    }
}